<?php
	require_once( "includes/common.inc.php" );
	
	$engine = new LogEngine();
	$template = new TemplateEngine();
	
	$arr = array ();
	$result = "";
	$isLogged = false;
	$isErr = false;
	
	$isSet = false;
	try 
	{
		$isSet = $engine->checkSet();
	}
	catch ( Exception $e )
	{
		$result .= $e->getMessage();
		$isErr = true;
	}
	if ( $isSet )
	{
		try 
		{
			$isLogged = $engine->findInBase( $_POST['email'], $_POST['password'] );
		}
		catch ( Exception $e )
		{
			$result .= $e->getMessage();
			$isErr = true;
		}
	}
	if ( $isLogged )
	{
		$resultArr = $engine->getNameByEmail( $_POST['email'] );
		$_SESSION['email'] = $_POST['email'];
		$_SESSION['firstName'] = $resultArr[0]['first_name'];
		$_SESSION['lastName'] = $resultArr[0]['last_name'];
		$isErr = false;
	}
	
	$vars = array( 
					'Title' => 'Login',
					'mode' => ( $isErr ) ? ( "Error" ) : ( "Succes" ),
					'message' => $result,
					'logScript' => checkLog().".php",
					'logMode' => checkLog(),
				 );
				 
	$vars['menu'] = printMenu();
	$vars['content'] = getView('templates/start_session.tpl', $vars);
	
	buildLayout('templates/head.tpl', $vars);
?>